package com.ccc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ccc.dao.StockDao;
import com.ccc.model.Product;
import com.ccc.model.Stock;
import com.ccc.model.User;
import com.ccc.util.dao.UtilDao;

@Component("stockDao")
public class StockDaoImpl implements StockDao {
	private UtilDao utilDao;

	@Override
	public void addStock(Stock stock) {
		utilDao.save(stock);
	}

	@Override
	public void deleteStock(Stock stock) {
		utilDao.delete(stock);
	}

	@Override
	public void updateStock(Stock stock) {
		utilDao.update(stock);
	}

	@Override
	public Stock findStock(int id) {
		return utilDao.find(Stock.class, id);
	}
	
	@Override
	public Stock findByUserProductTime(User user, Product product, Integer currentTime){
		List<Stock> list = utilDao.find(
				"from Stock s where s.user = ? and s.product = ? and s.currentTime = ?",
				new Object[]{user,product,currentTime});
		if(list==null||list.size()==0)
			return null;
		return list.get(0);
	}
	
	@Override
	public List<Stock> findByUserTime(User user, Integer currentTime){
		return utilDao.find(
				"from Stock s where s.user = ? and s.currentTime = ?",
				new Object[]{user, currentTime});
	}

	public UtilDao getUtilDao() {
		return utilDao;
	}

	@Resource
	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	

}
